package ym.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ym.mapper.HouseMapper;
import ym.mapper.HousePlotMapper;
import ym.pojo.House;
import ym.pojo.HousePlot;
import ym.service.HousePlotService;

import java.util.List;

@Transactional
@Service
public class HousePlotServiceImpl implements HousePlotService {
    @Autowired
    private HousePlotMapper housePlotMapper;
    @Autowired
    private HouseMapper houseMapper;

    @Override
    public void add(HousePlot housePlot) {
        housePlotMapper.insert(housePlot);
    }

    @Override
    public void update(HousePlot housePlot) {
        housePlotMapper.updateById(housePlot);
    }

    @Override
    public void delete(Integer id) {
        housePlotMapper.deleteById(id);
    }

    @Override
    public HousePlot findById(Integer id) {
        HousePlot housePlot = housePlotMapper.selectById(id);
        //封装房源列表【在租】
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("housePlotId", id).eq("houseUse", "1");
        List<House> houseList = houseMapper.selectList(queryWrapper);
        housePlot.setHouseList(houseList);
        //更新房源数量【在租】
        housePlot.setHousePlotNumber(String.valueOf(houseList.size()));
        return housePlot;
    }

    @Override
    public List<HousePlot> findAll() {
        return housePlotMapper.selectList(null);
    }

    @Override
    public Page<HousePlot> search(String search, int page, int size) {
        QueryWrapper<HousePlot> queryWrapper = new QueryWrapper<>();
        if (search != null) {
            queryWrapper.like("housePlotName", search);
        }
        return housePlotMapper.selectPage(new Page(page, size), queryWrapper);
    }
}
